package com.xxxy.hyc.xyxmp.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xxxy.hyc.xyxmp.R;
import com.xxxy.hyc.xyxmp.adapter.OrderItemAdapter;
import com.xxxy.hyc.xyxmp.bean.CartItem;
import com.xxxy.hyc.xyxmp.bean.Order;
import com.xxxy.hyc.xyxmp.utils.CartManager;

import java.util.List;

public class OrderActivity extends AppCompatActivity {

    private EditText etAddress, etRemark;
    private RadioGroup rgPayment;
    private RadioButton rbAlipay, rbWechat, rbAgriculturalBank, rbConstructionBank, rbCod;
    private RecyclerView rvOrderItems;
    private TextView tvTotalPrice;
    private Button btnSubmit;

    private List<CartItem> cartItems;
    private OrderItemAdapter orderItemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_order);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 显示返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        initViews();
        initData();
        initRecyclerView();
        updateTotalPrice();
    }

    private void initViews() {
        etAddress = findViewById(R.id.et_address);
        etRemark = findViewById(R.id.et_remark);
        rgPayment = findViewById(R.id.rg_payment);
        rbAlipay = findViewById(R.id.rb_alipay);
        rbWechat = findViewById(R.id.rb_wechat);
        rbAgriculturalBank = findViewById(R.id.rb_agricultural_bank);
        rbConstructionBank = findViewById(R.id.rb_construction_bank);
        rbCod = findViewById(R.id.rb_cod);
        rvOrderItems = findViewById(R.id.rv_order_items);
        tvTotalPrice = findViewById(R.id.tv_total_price);
        btnSubmit = findViewById(R.id.btn_submit);

        btnSubmit.setOnClickListener(v -> submitOrder());
    }

    private void initData() {
        cartItems = CartManager.getInstance().getCartItems();
    }

    private void initRecyclerView() {
        orderItemAdapter = new OrderItemAdapter(cartItems);
        rvOrderItems.setLayoutManager(new LinearLayoutManager(this));
        rvOrderItems.setAdapter(orderItemAdapter);
    }

    private void updateTotalPrice() {
        double total = CartManager.getInstance().getTotalPrice();
        tvTotalPrice.setText("合计：¥" + String.format("%.2f", total));
    }

    private void submitOrder() {
        String address = etAddress.getText().toString().trim();
        String remark = etRemark.getText().toString().trim();
        
        // 获取选中的支付方式
        int selectedPaymentId = rgPayment.getCheckedRadioButtonId();
        int paymentMethod = 0; // 默认支付宝
        String paymentMethodName = "支付宝";

        if (selectedPaymentId == R.id.rb_alipay) {
            paymentMethod = 0;
            paymentMethodName = "支付宝";
        } else if (selectedPaymentId == R.id.rb_wechat) {
            paymentMethod = 1;
            paymentMethodName = "微信支付";
        } else if (selectedPaymentId == R.id.rb_agricultural_bank) {
            paymentMethod = 2;
            paymentMethodName = "中国农业银行";
        } else if (selectedPaymentId == R.id.rb_construction_bank) {
            paymentMethod = 3;
            paymentMethodName = "中国建设银行";
        } else if (selectedPaymentId == R.id.rb_cod) {
            paymentMethod = 4;
            paymentMethodName = "货到付款";
        }

        if (address.isEmpty()) {
            Toast.makeText(this, "请输入收货地址", Toast.LENGTH_SHORT).show();
            return;
        }

        // 创建订单对象
        Order order = new Order(cartItems, address, remark, paymentMethod);

        // 这里应该调用提交订单的接口，我们暂时用Toast提示
        Toast.makeText(this, "订单已提交\n支付方式：" + paymentMethodName, Toast.LENGTH_LONG).show();

        // 清空购物车
        CartManager.getInstance().clearCart();

        // 返回购物车页面并刷新
        finish();
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}